﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;

namespace CuaHangBangDia.QLKHThue
{
    public partial class TraDiaThueForm : Form
    {
        public TraDiaThueForm()
        {
            InitializeComponent();
        }
        private int item = 20;
        private int tong;
        private int page = 1;
        public static int TongTrang(int soluongdl, int num)
        {
            int kq = (int)(soluongdl / num);
            if (kq * num == soluongdl)
                return kq;
            return kq + 1;
        }
        private void LoadKH(List<KhachHangThue> kh)
        {
            dgvTraDiaThue.DataSource = kh;
            tong = TongTrang(KhachHangThue.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }
        private void LoadCTKH(List<ChiTietKhachHangThue> kh)
        {
            dgvTraDiaThue.DataSource = kh;
            tong = TongTrang(ChiTietKhachHangThue.Objects.GetAll().ToList().Count, item);
            txtTrang.Text = Convert.ToString(page);
            lbTongTrang.Text = "/ " + tong;

        }
        public List<ChiTietKhachHangThue> getPageCT(int page)
        {
            var query = from c in ChiTietKhachHangThue.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }
        public List<KhachHangThue> getPageKh(int page)
        {
            var query = from c in KhachHangThue.Objects.GetAll().ToList()
                           .Skip(item * page - item).Take(item)
                        select c;
            return query.ToList();

        }


        private void btnTim_Click(object sender, EventArgs e)
        {
            List<TraDiaThue> kq = LayTTTraDiaThue();
            dgvTraDiaThue.DataSource = kq;
            gbPhanTrang.Visible = false;

        }

        private List<TraDiaThue> LayTTTraDiaThue()
        {
            List<int> ma = new List<int>();
            List<TraDiaThue> traDia = new List<TraDiaThue>();
            string info = txtTimKiem.Text;
            if (info != "")
            {
                List<KhachHangThue> list = KhachHangThue.Objects.Find(f => f.TenKhachHang.Contains(info)).ToList();
                if (list.Count > 0)
                {
                    foreach (KhachHangThue kh in list)
                    {
                        ma.Add(kh.MaKhachHang);
                    }
                    foreach (int m in ma)
                    {
                        List<ChiTietKhachHangThue> listct = ChiTietKhachHangThue.Objects.Find(t => t.MaKhachHang == m).ToList();
                        foreach (ChiTietKhachHangThue item in listct)
                        {
                            int mbd = item.MaBangDia;
                            BangDia bd = BangDia.Objects.GetById(mbd);
                            TraDiaThue td = new TraDiaThue();
                            td.MaKhachHang = m;
                            td.TenBangDia = bd.TenBangDia;
                            td.SoLuong = item.SoLuong;
                            td.TienCoc = item.TienCoc;
                            txtGhiChu.Text = item.GhiChu;
                            traDia.Add(td);
                        }
                    }
                }
                else
                    MessageBox.Show("Không tìm thấy khách hàng có thông tin là "+info+"!", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("Chưa nhập thông tin", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return traDia;
        }

        private void TraDiaThueForm_Load(object sender, EventArgs e)
        {
            gbPhanTrang.Visible = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvTraDiaThue.Rows.Count; i++)
                {
                    if (dgvTraDiaThue.Rows[i].Cells[0].Value != null)
                    {
                        int ma = Convert.ToInt32(dgvTraDiaThue.Rows[i].Cells[1].Value.ToString());
                        ChiTietKhachHangThue.Objects.Delete(m => m.MaKhachHang == ma);
                        KhachHangThue.Objects.Delete(k => k.MaKhachHang == ma);
                        string tenbd = dgvTraDiaThue.Rows[i].Cells[2].Value.ToString();
                        List<BangDia> bd = BangDia.Objects.Find(b => b.TenBangDia == tenbd).ToList();
                        foreach (BangDia bangdia in bd)
                        {
                            BangDia b = new BangDia();
                            b.MaBangDia = bangdia.MaBangDia;
                            b.TenBangDia = bangdia.TenBangDia;
                            b.MaLoai = bangdia.MaLoai;
                            b.MaTheLoai = bangdia.MaTheLoai;
                            b.Gia = bangdia.Gia;
                            int sl = Convert.ToInt32(dgvTraDiaThue.Rows[i].Cells[3].Value);
                            b.SoLuong = bangdia.SoLuong + sl;
                            BangDia.Objects.Update(b);
                        }
                        MessageBox.Show("Thu Lại đĩa thành công", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnTim.PerformClick();                            
                    }
                    
                }
                
                
            }
            catch (System.Exception)
            {
                MessageBox.Show("Xảy ra lỗi", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTDau_Click(object sender, EventArgs e)
        {

        }

        private void btnLui_Click(object sender, EventArgs e)
        {

        }

        private void btnTien_Click(object sender, EventArgs e)
        {

        }

        private void btnTCuoi_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dgvTraDiaThue_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            bool _selectAll = false;
            int n = dgvTraDiaThue.Rows.Count;
            if (e.ColumnIndex == 0 && n > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < n; idx++)
                {
                    dgvTraDiaThue.Rows[idx].Selected = _selectAll;
                    dgvTraDiaThue.Rows[idx].Cells[0].Value = _selectAll;
                }
                dgvTraDiaThue.Rows[n - 1].Selected = false;
                dgvTraDiaThue.Rows[n - 1].Cells[0].Value = false;
            }
        }
    }
}
